package com.zkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkj.constant.MessageConstant;
import com.zkj.constant.StatusConstant;
import com.zkj.dto.CategoryDTO;
import com.zkj.dto.CategoryPageQueryDTO;
import com.zkj.entity.Category;
import com.zkj.exception.DeletionNotAllowedException;
import com.zkj.mapper.CategoryMapper;
import com.zkj.mapper.DishMapper;
import com.zkj.mapper.SetmealMapper;
import com.zkj.result.Result;
import com.zkj.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService  {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.Disabled);
        categoryMapper.insert(category);
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    public Result pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        IPage<Category> page = new Page<>(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();

        if (categoryPageQueryDTO.getName() != null) {
            objectQueryWrapper.like("name", categoryPageQueryDTO.getName());
        }
        if (categoryPageQueryDTO.getType() != null) {
            objectQueryWrapper.eq("type", categoryPageQueryDTO.getType());
        }

        objectQueryWrapper.orderByAsc("sort");
        IPage<Category> categoryIPage = categoryMapper.selectPage(page, objectQueryWrapper);
        return Result.success(categoryIPage);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        log.info("eleteById1: {}", count);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        log.info("eleteById2: {}", count);

        //删除分类数据
        categoryMapper.deleteById(id);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.updateById(category);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder().id(id).status(status).build();
        categoryMapper.updateById(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    public List<Category> list(Integer type) {
        QueryWrapper<Category> objectQueryWrapper = new QueryWrapper<>();
        if (type != null) {
            objectQueryWrapper.eq("type", type);
        }
        objectQueryWrapper.orderByAsc("sort");
        return categoryMapper.selectList(objectQueryWrapper);
    }
}
